/*
 * Copyright (c) 2017, Li Bo/Alex Lee All rights reserved.
 * 
 * http://www.libo4dream.club/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package com.hyts.fscan.scanner;


import com.hyts.fscan.base.AbstractFileSystemScanner;
import com.hyts.fscan.base.FileScannerCenter;
import com.hyts.fscan.domain.Directory;
import com.hyts.fscan.filter.BaseLoadFileFilter;
import com.hyts.fscan.validate.DirectoryRuleValidate;

import java.io.File;

/**
 * @ClassName: SimpleFScanner
 * @Description: 简单式扫描方法，扫描器服务
 * @author: LiBo/Alex Lee
 * @date: 2017年9月27日 上午12:49:55
 * @email: alexlibochn@china-pui.com.cn/alex.link@foxmail.com
 */
public abstract class SimpleFScanner extends AbstractFileSystemScanner {

	/* (non Javadoc)
	 * @Title: scan
	 * @Description: 文件扫描器
	 * @param dir
	 * @return
	 * @see com.libo.scan.base.AbstractFileSystemScanner#scan(com.libo.scan.domain.Directory)
	 */
	@Override
	public Integer scan(Directory dir) {
		boolean flag = DirectoryRuleValidate.valiateDirectory(dir);
		if(flag){
			File dirFile = new File(dir.getDirectoryPath());
			//校验是否符合要求的文件
			if(dirFile.exists()){
				BaseLoadFileFilter filter = new BaseLoadFileFilter(dir);
				File[] fileList = dirFile.listFiles(filter);
				for(File file:fileList){
					if(file.isDirectory()){
						Directory directory = new Directory();
						directory.setDirectoryPath(file.getPath());
						scan(directory);
					}
					else
					{
						FileScannerCenter.dealGetFileCurrentNum.incrementAndGet();
						FileScannerCenter.dealGetFilesList.add(file);
						this.doWork(file);
					}
				}
			}
			else{
				throw new RuntimeException("传入的文件不存在或者存在问题!"+dirFile.getAbsolutePath());
			}
		}
		return FileScannerCenter.dealGetFileCurrentNum.get();
	}
	
	public abstract Object doWork(File dir);


}
